﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WG计划管理工具
{
    public partial class frmCPBMSelector : Form
    {
        static string strSelected;
        SqlTools.SqlTools sqt;

        public frmCPBMSelector()
        {
            InitializeComponent();
        }

        public static string ShowForm(Form frmowner)
        {
            frmCPBMSelector newFrmCPBMSelector = new frmCPBMSelector();
            newFrmCPBMSelector.Owner = frmowner;
            frmowner.Enabled = false;
            newFrmCPBMSelector.ShowDialog ();
            return strSelected;
        }

        private void txtKeyWords_TextChanged(object sender, EventArgs e)
        {
            if (txtKeyWords.Text.Trim().Length > 0 && txtKeyWords .Text .Trim ()!="%")
            {
                DataTable tmpdt = sqt.GetDataTable("select ycl001 产品编码 from ycl_table where ycl017='M' and ycl001 like '" + txtKeyWords.Text.Trim() + "' order by ycl001");
                lstCPBMList.DataSource = tmpdt;
                lstCPBMList.DisplayMember = "产品编码";
            }
            else
            {
                lstCPBMList.DataSource = null;
                lstCPBMList.DisplayMember = string.Empty;
            }
        }

        private void frmCPBMSelector_Load(object sender, EventArgs e)
        {
            sqt = new SqlTools.SqlTools(WG计划管理工具.Properties.Settings.Default.ConnectionString);
            strSelected = string.Empty;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtKeyWords.Text = string.Empty;
        }

        private void frmCPBMSelector_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Owner.Enabled = true;
        }

        private void lstCPBMList_DoubleClick(object sender, EventArgs e)
        {
            if (lstCPBMList.SelectedItem != null)
            {
                strSelected =((DataRowView ) lstCPBMList.SelectedItem).Row .ItemArray [0].ToString ();
                this.Close();
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            strSelected = txtKeyWords.Text.Trim();
            this.Close();
        }
    }
}